package com.slz3.business.module.goodsManagement.gooOutWarehouse.domain;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.util.LocalDT;
import com.slz3.business.frame.util.UUIDUtils;
import com.slz3.business.frame.util.VE;
import org.springframework.util.MultiValueMap;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.stream.Collectors;

public class GooOutWarehouseFactory {

    /**
     * 初始化基础属性值
     */
    public static GooOutWarehouse initBaseFields(GooOutWarehouse gooOutWarehouse) {
        if (gooOutWarehouse.getUuid() == null) {
            gooOutWarehouse.setUuid(UUIDUtils.generateUUID());
            gooOutWarehouse.setCreateTime(LocalDateTime.now());
            if (VE.isEmpty(gooOutWarehouse.getSerAudit())) {
                gooOutWarehouse.setSerAudit(AuditFlag.WAIT_SUBMIT);
            }
            if (VE.isEmpty(gooOutWarehouse.getSerUsing())) {
                gooOutWarehouse.setSerUsing(UsingFlag.ENABLE);
            }
        } else {
            gooOutWarehouse.setLastModifyTime(LocalDateTime.now());
        }
        return gooOutWarehouse;
    }

    /**
     * 初始化基础属性值
     */
    public static GooOutWarehouse initBaseFields() {
        return initBaseFields(newTemStyle());
    }

    public static GooOutWarehouse newTemStyle() {
        return new GooOutWarehouse();
    }

    public static GooOutWarehouseCondition newGooOutWarehouseCondition(MultiValueMap<String, String> params) {

        GooOutWarehouseCondition condition = new GooOutWarehouseCondition();

        String createTimeOUTSET = params.getFirst("createTimeOUTSET");
        String createTimeFINISH = params.getFirst("createTimeFINISH");
        String lastModifyTimeOUTSET = params.getFirst("lastModifyTimeOUTSET");
        String lastModifyTimeFINISH = params.getFirst("lastModifyTimeFINISH");
        String serAuditList = params.getFirst("serAuditList");

        condition.setCreateTimeOUTSET(VE.isEmpty(createTimeOUTSET) ? null : LocalDT.parseStringToDateTime(createTimeOUTSET, GooOutWarehouseCondition.CREATE_TIME_OF_PATTERN));
        condition.setCreateTimeFINISH(VE.isEmpty(createTimeFINISH) ? null : LocalDT.parseStringToDateTime(createTimeFINISH, GooOutWarehouseCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeOUTSET(VE.isEmpty(lastModifyTimeOUTSET) ? null : LocalDT.parseStringToDateTime(lastModifyTimeOUTSET, GooOutWarehouseCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeFINISH(VE.isEmpty(lastModifyTimeFINISH) ? null : LocalDT.parseStringToDateTime(lastModifyTimeFINISH, GooOutWarehouseCondition.CREATE_TIME_OF_PATTERN));
        condition.setSerAuditList(VE.isEmpty(serAuditList) ? null : Arrays.stream(serAuditList.split(",")).map(item -> AuditFlag.valueOf(item)).collect(Collectors.toList()));

        return condition;

    }

}
